/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.util;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDFS;
import edu.yonsei.iwb.sdt.datastructure.TargetInstance;
import java.util.Enumeration;
import java.util.Vector;
import edu.yonsei.iwb.sdt.datastructure.TargetInstanceValueInformation;
import edu.yonsei.iwb.sdt.datastructure.tree.DEdge;
import edu.yonsei.iwb.sdt.datastructure.tree.DNode;
import edu.yonsei.iwb.sdt.datastructure.tree.NodeType;
import edu.yonsei.iwb.sdt.guix.GDLearningPanel;
import edu.yonsei.iwb.sdt.guix.GraphDataLearner;
import java.text.SimpleDateFormat;

/**
 *
 * @author JDK
 */
public class UtilFunctions {
    
    public static void showTree(DNode node){
        GDLearningPanel gdp = new GDLearningPanel(new GraphDataLearner());
        gdp.main(node);
    }
    
    public static void printNode(DNode node) {
//        String s = new String();        
        for (int i = 0; i < node.getEdge().size(); i++) {
            System.out.println("---- " +node.getEdge().size());
            DEdge edge = node.getEdge().get(i);
            SDTLog.log(SDTLog.BASIC, edge.toString());
            
            if (edge.getChildNode().NODE_TYPE == NodeType.BRANCHNODE) {
                System.out.println(edge.getChildNode().NODE_TYPE+" -- " + NodeType.BRANCHNODE);
                SDTLog.log(SDTLog.BASIC, edge.getChildNode().toString());
                printNode(edge.getChildNode());
            } else {
                System.out.println(edge.getChildNode().NODE_TYPE+" -- " + NodeType.LEAFNODE);
                SDTLog.log(SDTLog.BASIC, edge.getChildNode().toString());
            }
        }
    }
            
    public static Vector<Resource> getSubClasses(Resource r, Model model){
        Vector<Resource> rset = new Vector();        
        ResIterator rit = model.listResourcesWithProperty(RDFS.subClassOf, r);
        while(rit.hasNext()){
            rset.add(rit.nextResource());
        }        
        return rset;
    }
    
    public static Vector<Resource> copyInstanceSet(Vector<Resource> oldset){
        Vector<Resource> newset = new Vector();
        for (int i = 0; i < oldset.size(); i++) {
            newset.add(oldset.get(i));            
        }
        return newset;
    }
    
    public static Vector<Resource> getSubjectResourceVector(StmtIterator st){
        Vector<Resource> rvector = new Vector<Resource>();
        
        while(st.hasNext()){
            Statement s = st.nextStatement();
            Resource r = s.getSubject();
            rvector.add(r);
        }
        
        return rvector;
    }
    
    public static Vector<Resource> getSubjectResourceVector(ResIterator res){
        Vector<Resource> rvector = new Vector<Resource>();
        
        while(res.hasNext()){            
            Resource r = res.nextResource();
//            if(!rvector.contains(r)){
                rvector.add(r);                
//            }
        }
        
        return rvector;
    }
    
    public static void printVectorOfResource(Vector<Resource> rv){
        System.out.println("----------------- Contained List of Resource --------------------");
        for (int i = 0; i < rv.size(); i++) {
            Resource r = rv.get(i);
            System.out.println(r.toString());
        }        
    }
    
//    public static double calculateEntropy(TrueFalseInformation tis){
//        double e = 0.0;
//        
//        double trueInsnum = tis.getTrueInsSize();
//        double falseInsnum = tis.getFalseInsSize();
//        double totalInsnum = tis.getTrueInsSize()+tis.getFalseInsSize();
//        
////        System.out.println("true ins num = " + trueInsnum);
////        System.out.println("false ins num = " + falseInsnum);
////        System.out.println("total ins num = " + totalInsnum);
//        
//        e = -(trueInsnum/totalInsnum)*logB(trueInsnum/totalInsnum)-(falseInsnum/totalInsnum)*logB(falseInsnum/totalInsnum);
////        System.out.println("Entropy = "+e);
//        return e;
//    }
    
    public static double calculateEntropy(TargetInstanceValueInformation tivi, int totalTInum){
        double e = 0.0;
        double totalInsnum = (double) totalTInum;
                
        Enumeration<Integer> keye = tivi.keys();
        
        while(keye.hasMoreElements()){
//            System.out.println("is working");
            int key = keye.nextElement();
            Vector<TargetInstance> rset = tivi.get(key);
            double valueInsnum = rset.size();
//            System.out.println("value ins num = " + valueInsnum);
//            System.out.println("total ins num = " + totalInsnum);
            
            e = e - (valueInsnum/totalInsnum)*logB(valueInsnum/totalInsnum);
        }
        
//        System.out.println("Entropy = "+e);
        return e;
    }
    
    private static double getInstanceSize(TargetInstanceValueInformation tivi){
        double size = 0;
        
        Enumeration<Integer> keye = tivi.keys();
        
        while(keye.hasMoreElements()){
            int key = keye.nextElement();
            Vector<TargetInstance> rset = tivi.get(key);
            size += rset.size();            
        }
        
        return size;
    }
    
    private static double logB(double x) {
             if(x==0){
                 return 0.0;
             }
             return Math.log(x) / Math.log(2);
    }
        
}
